package com.slaffka.controllers;

import com.slaffka.controllers.exceptions.PageNotFoundException;
import com.slaffka.model.Page;
import com.slaffka.model.user.User;
import com.slaffka.services.ServiceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: v.tsapaev
 * Date: 27.02.12
 * Time: 18:28
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/admin")
@SessionAttributes("newPage")
public class AdminController {
    
    private static Logger LOG = LoggerFactory.getLogger(AdminController.class);

    @Autowired
    ServiceFactory service;

    @RequestMapping(value = "/{url}", method = RequestMethod.GET)
    public String main(@PathVariable String url, Model model) {
        LOG.info("url = /" + url);

        List<Page> adminPages = service.getPageService().getAllAdminPages();
        List<Page> appPages = service.getPageService().getAllApplicationPages();
        if (adminPages != null){
            for (Page page: adminPages){
                LOG.info("url = " + url + "page = " + page);
                if (url.equals(page.getUrl())){
                    model.addAttribute(new User());
                    model.addAttribute("pages", adminPages);
                    model.addAttribute("appPages", appPages);
                    model.addAttribute("currentPage", page);
                    model.addAttribute("newPage", new Page());
                    return "jsp/admin/admin";
                }
            }
        }
        throw new PageNotFoundException(url);
    }

    @RequestMapping(value = "/registerUser", method = RequestMethod.POST, headers = {"content-type=application/json"})
    public @ResponseBody String getUsers(@RequestBody User user){
        LOG.debug(user.toString());
        service.getUserService().createUser(user);
        return user.getUsername();
    }

    @RequestMapping(value = "/createPage", method = RequestMethod.POST, headers = {"content-type=application/json"})
    public @ResponseBody Page createPage(@RequestBody Page page, Model model){
        LOG.info(page.toString());
        return service.getPageService().createPage(page);
    }

    @RequestMapping(value = "/updatePage", method = RequestMethod.POST, headers = {"content-type=application/json"})
    public @ResponseBody Page updatePage(@RequestBody Page page, Model model){
        LOG.info(page.toString());
        return service.getPageService().updatePage(page);
    }

    @RequestMapping(value = "/removePage", method = RequestMethod.POST)
    public @ResponseBody Boolean removePage(@RequestParam String pageId){
        LOG.info(pageId);
        return service.getPageService().removePage(pageId);
    }

    @RequestMapping(value = "/getPageById", method = RequestMethod.POST)
    public @ResponseBody Page getPageById(@RequestParam String pageId){
        LOG.info(pageId);
        return service.getPageService().getPageById(pageId);
    }

    @RequestMapping(value = "/clearCache", method = RequestMethod.POST)
    public @ResponseBody String clearCache(Model model){
        LOG.info("url = /clearCache");
        return service.getAdminService().clearCache().toString();
    }

    @RequestMapping(value = "/getPageModel", method = RequestMethod.POST)
    public @ResponseBody String getUserModel(Model model){
        LOG.info("url = /getUserModel");
        model.addAttribute("newPage", new Page());
        return "";
    }

    @ExceptionHandler(PageNotFoundException.class)
    @ResponseStatus(value=HttpStatus.NOT_FOUND,reason="Contact not found")
    public void notFound() {
        LOG.info("page not found");
    }


}
